package com.junbao.module.main

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.junbao.module.main.kt.Person
import com.junbao.module.main.kt.Student
import kotlinx.coroutines.*
import java.util.*

/**
 *    author : Majunbao
 *    github : https://github.com/MaJunBaox
 *    time   : 2022/8/28 10:34 上午
 *    desc   : kotlin学习
 */

/**
 * Kotlin中==和java中的equals是一样的
 * Kotlin中===和java中==效果一样
 */
const val NAME: String = "Majunbao"
const val TAG: String = "liuyi"

class KotlinActivity : AppCompatActivity() {
    //伴生对象
    companion object {
        const val TEXT: String = "majunbao"
    }

    override  fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin)
//        toast(text = "马俊宝")
//        toast(this ,"majunbao")
//        toast(this,"ma",Toast.LENGTH_SHORT)
//
//        Log.d(TAG, "onCreate: "+TAG.toMa())
//        Log.d(TAG, "${result(1,2)}")

//        println("测试开始"+(Thread.currentThread() == Looper.getMainLooper().thread))
//        runBlocking {
//            println("测试延迟开始"+(Thread.currentThread() == Looper.getMainLooper().thread))
//            delay(2000)
//            println("测试延迟结束")
//        }
//        println("测试结束")


//        println("测试开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
//        GlobalScope.launch {
//            println("测试延迟开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
//            delay(20000)
//            println("测试延迟结束")
//        }
//        println("测试结束")


//        println("测试开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
//        val async = GlobalScope.async {
//            println("测试延迟开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
//            delay(20000)
//            println("测试延迟结束")
//            "66666"
//        }
//        println("测试结束")
//        suspend {
//             println("测试返回值${async.await()}")
//        }


      //  launch()
            //asycn()
     //   runBlocking()

//        runBlocking{
//           // suspend()
//            asycn()
//        }


    }

    /**
     * launch
     * 不是按照顺序执行的
     * 没有返回值
     */
    private fun launch() {
        println("测试开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
        GlobalScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                println("测试是否是主线成 " + (Thread.currentThread() == Looper.getMainLooper().thread))
                delay(5000)
            }
            println("测试延迟开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
            delay(10000)
            println("测试延迟结束")
        }
        println("测试结束")
    }


    /**
     * 阻塞线程 按顺序执行
     */
    private fun runBlocking() {
        println("测试开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
        runBlocking (Dispatchers.IO){
            println("测试延迟开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
            delay(2000)
            println("测试延迟结束")
        }
        println("测试结束")
    }


    private suspend fun asycn(){
        println("测试开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
      val async=   GlobalScope.async {
            println("测试延迟开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
            delay(10000)
            println("测试延迟结束")
            return@async "majunbao"
        }
        println("测试结束")
        println(async.await())
    }

    private suspend fun suspend(){
        println("测试延迟开始 " + (Thread.currentThread() == Looper.getMainLooper().thread))
        delay(2000)
        println("测试延迟结束")
    }

    var name = "1123"

    inner class MyTask {
        var name = "mmmm"
        fun test() {
            Log.d(TAG, "test: $name ====" + this@KotlinActivity.name)
        }
    }


    private fun learn1() {
        val person = Person("马俊宝", 20)
        Log.d("liuyi", "${person}")
        var student = Student("男", "18910204379", "ma", 20)
        Log.d("liuyi", "onCreate: $student")

        //var intent = Intent(this , MainActivity::class.java)

        if (student is Person) {
            Log.d(TAG, "student 是 Person类型")
        } else {
            Log.d(TAG, "student 不是 Person类型")
        }

        //比较内容是否是一样的
        if ("Majunbao" == NAME) {
            Log.d(TAG, "相等")
        }

        //比较两个对象是否是 同一个
        Log.d(TAG, (student === person).toString())




        student.age
    }

    fun test(): Unit {
        run {

        }
    }

    fun test1() {

    }

    fun test2(name: String) {

    }

    fun test3(num: Int): Int {
        return num
    }


    fun toast(context: Context = this, text: String, time: Int = Toast.LENGTH_SHORT) {
        Toast.makeText(context, text, time)
    }


    fun String.toMa(): String {
        return this + "MAjunbao"
    }

    var result = fun(number1: Int, number2: Int): Int {
        return number1 + number2
    }

}